//下拉框选项
bbsApp.factory("lyfUtils", ["$rootScope", "$http", function($rootScope, $http){
	"use strict";
	var utils = {
		//弹窗
		showDialog: function(type, title, msg, callback){
			if(type == "alert" || type == "result"){
				BootstrapDialog.show({
					size: BootstrapDialog.SIZE_SMALL,
					title: title,
		            message: msg,
		            closable: true,
		            closeByBackdrop: false,
		            closeByKeyboard: false,
		            buttons: [{
		                label: "关闭",
		                action: function(dialogRef){
		                    dialogRef.close();
		                }
		            }],
					onhide: function(dialogRef){
						if(callback){
							callback();
						}
					}
		        });
			}
		},
		//获取长度
		getStrLength : function(str){
			var cArr = str.match(/[^\x00-\xff]/ig);
			return str.length + (cArr == null ? 0 : cArr.length);
		},
		//confirm提示框
		confirm : function(title, message, callBack){
			 BootstrapDialog.confirm({
		            title: "<h4>" + title + "</h4>",
		            message: '<h3 class="modal-body text-center">' + message + '</h3>',
		            closable: false,
		            draggable: true,
		            btnCancelLabel: '取消',
		            btnOKLabel: '确定',
		            callback:callBack
		       });
		},
		//copy采购单
		copyPurchaseOrder : function(purchaseId, callBack){
			$http.post('purchaseInfo/copyPurchaseOrderById.do',{purchaseId : purchaseId}).success(function(result){
				if($.isFunction(callBack)){
					callBack(result);
				}
			});
		},
		//选择城市区域 （省、市、县）
		queryAreaByParentCode : function(url,param,callback){
			$http.post(url, angular.toJson(param)).success(function(result) {
				callback(result);
			})
		},
	};
	/**
	 * 将bootstrap控件标记为成功
	 */
	utils.markSuccess = function(id, scope){
		scope[id + 'Group'] = "has-success has-feedback";
		scope[id + 'Icon'] = "glyphicon-ok glyphicon form-control-feedback";
	};

	/**
	 * 将bootstrap控件标记为失败
	 */
	utils.markError = function(id, scope){
		scope[id + 'Group'] = "has-error has-feedback";
		scope[id + 'Icon'] = "glyphicon-remove glyphicon form-control-feedback";
	};
	
	/**
     * 表格选中控制
     */
    utils.doChecked = {
    	init: function(data){//对数据进行初始化
	  		if(data){
	  			angular.forEach(data, function(data, index, array){
	  				data.$isChecked = false;
	  			});
	  			return {data: data, allChecked: false};
	  		}
    	},
    	singleReverseChecked: function(item, allData){//单个反选
    		if(!allData){
    			return;
    		}
    		var allChecked = false;
    		if(item.$isChecked == true){
    			var flag = true;
    			angular.forEach(allData, function(data, index, array){
	    	  		if(data.$isChecked == false){
	    	  			flag = false;
	    	  			return false;
    	  		}});
	        	allChecked = flag;
	        	
    		}
    		return allChecked;
    		
    	},
    	allReversechecked: function(allCheckedStatus, allData){//反选
    		if(!allData){
    			return;
    		}
    		angular.forEach(allData, function(data, index, array){
    			data.$isChecked = allCheckedStatus;
  			});
    		return allData;
    	}
  	};  
	return utils;
}]);